package org.southyin.securitysamples.exceptions;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.southyin.securitysamples.commons.CommonsResp;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLException.class)
    public CommonsResp handler(SQLException e) {
        if (e instanceof MySQLIntegrityConstraintViolationException) {
            return CommonsResp.failed(500,"操作失败,有关联数据");
        }
        return CommonsResp.failed(500,"数据库异常,操作失败");
    }
}
